package com.yra.config;

import com.yra.domain.Admin;
import com.yra.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class UserDetailService implements UserDetailsService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 模拟一个用户，替代数据库获取逻辑
        Admin admin = adminMapper.getUserByName(username);
        if (admin!=null){
            admin.setPassword(this.passwordEncoder.encode(admin.getPassword()));
            System.out.println(admin.getPassword());
            return new User(username,admin.getPassword(),true,true,true,true, AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN"));
        }
        return null;
    }
}
